package com.jwgf.smarowindingmachine_unity.ui;

import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.jwgf.adapter.CommonAdapter;
import com.jwgf.adapter.ViewHolder;
import com.jwgf.common.ViewCache;
import com.jwgf.report.ShiftInfo;
import com.jwgf.report.ShiftTimeRule;
import com.jwgf.smarowindingmachine_unity.R;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class ProductionManagementChangeClassSettingFragment extends BaseFragment implements OnItemClickListener {

	private static final String CLASSWEEKNUM = "class_week_num";
	private static final String CLASSSHIFTNUM = "class_shift_num";
	private static final String CLASSSHIFTSTARTTIME = "class_shift_start_time";
	private GridView gvSwitchClassTable;
	private List<HashMap<String, String>> gvList = new ArrayList<HashMap<String, String>>();
	private CommonAdapter gvAdapter;
	private EditClassSettingDialog editClassSettingDia;
	private String[] strWeekDay;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		gvSwitchClassTable = (GridView) getActivity().findViewById(R.id.switch_class_gv);
		initPage();

	}

	private void initPage() {
		strWeekDay = getActivity().getResources().getStringArray(R.array.report_weekday);
		getGVListData();
		gvSwitchClassTable.setAdapter(gvAdapter = new CommonAdapter<HashMap<String, String>>(getActivity(), gvList,
				R.layout.item_switch_class_gv) {

			@Override
			public void convert(ViewHolder holder, HashMap<String, String> item) {
				holder.setText(R.id.switch_class_week, item.get(CLASSWEEKNUM));
				holder.setText(R.id.switch_class_class_one, item.get(getClassShiftNumKey(0)));
				holder.setText(R.id.switch_class_class_two, item.get(getClassShiftNumKey(1)));
				holder.setText(R.id.switch_class_class_three, item.get(getClassShiftNumKey(2)));
				holder.setText(R.id.switch_class_class_four, item.get(getClassShiftNumKey(3)));
				holder.setText(R.id.switch_class_time_one, item.get(getClassShiftStartTimeKey(0)));
				holder.setText(R.id.switch_class_time_two, item.get(getClassShiftStartTimeKey(1)));
				holder.setText(R.id.switch_class_time_three, item.get(getClassShiftStartTimeKey(2)));
				holder.setText(R.id.switch_class_time_four, item.get(getClassShiftStartTimeKey(3)));
			}
		});

		gvSwitchClassTable.setOnItemClickListener(this);
	}

	protected String getClassShiftStartTimeKey(int index) {
		return CLASSSHIFTSTARTTIME + index;
	}

	private void getGVListData() {
		gvList.clear();
		// 初始化班次和开始时刻
		// 周一到周六
		for (int i = Calendar.MONDAY; i <= Calendar.SATURDAY; i++) {
			ArrayList<ShiftInfo> infoList = ShiftTimeRule.getInstance().getDayInfo(i).getAllShiftInfo();
			HashMap<String, String> item = new HashMap<String, String>();
			int weekDayIndex = (i + 5) % 7;
			item.put(CLASSWEEKNUM, strWeekDay[weekDayIndex]);
			for (int j = 0; j < 4; ++j) {
				item.put(getClassShiftNumKey(j), String.valueOf(infoList.get(j).getNum()));
				if (0 == infoList.get(j).getNum()) {
					item.put(getClassShiftStartTimeKey(j), "--:--");
				} else {
					item.put(getClassShiftStartTimeKey(j),
							(infoList.get(j).getHour() < 10 ? ("0" + infoList.get(j).getHour())
									: infoList.get(j).getHour()) + ":"
									+ (infoList.get(j).getMinute() < 10 ? ("0" + infoList.get(j).getMinute())
											: infoList.get(j).getMinute()));
				}
			}
			gvList.add(item);
		}
		// 周日
		ArrayList<ShiftInfo> infoList = ShiftTimeRule.getInstance().getDayInfo(Calendar.SUNDAY).getAllShiftInfo();
		HashMap<String, String> item = new HashMap<String, String>();
		item.put(CLASSWEEKNUM, strWeekDay[(Calendar.SUNDAY + 5) % 7]);
		for (int j = 0; j < 4; ++j) {
			item.put(getClassShiftNumKey(j), String.valueOf(infoList.get(j).getNum()));
			if (0 == infoList.get(j).getNum()) {
				item.put(getClassShiftStartTimeKey(j), "--:--");
			} else {
				item.put(getClassShiftStartTimeKey(j),
						(infoList.get(j).getHour() < 10 ? ("0" + infoList.get(j).getHour()) : infoList.get(j).getHour())
								+ ":" + (infoList.get(j).getMinute() < 10 ? ("0" + infoList.get(j).getMinute())
										: infoList.get(j).getMinute()));
			}
		}
		gvList.add(item);
	}

	private String getClassShiftNumKey(int index) {
		return CLASSSHIFTNUM + index;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = ViewCache.getInstance().getView(R.layout.fragment_production_management_change_class_setting);
		return v;
	}

	@Override
	public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
		editClassSettingDia = new EditClassSettingDialog(getActivity(), position);
		editClassSettingDia.show();
		editClassSettingDia.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface arg0) {
				getGVListData();
				gvAdapter.notifyDataSetChanged();
			}
		});
	}

}
